home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_08 / 9n08016b < prev    next >
Text File  |  1991-07-08  |  3KB  |  68 lines

  1. /* _Dscale function -- IEEE 754 version */
  2. #include <169>xmath.h"
  3.  
  4. short _Dscale(double *px, short xexp)
  5.         {       /* scale *px by 2^xexp with checking */
  6.         long lexp;
  7.         unsigned short *ps = (unsigned short *)px;
  8.         short xchar = (ps[_D0] & _DMASK) >> _DOFF;
  9.  
  10.         if (xchar == _DMAX)     /* NaN or INF */
  11.                 return (ps[_D0] & _DFRAC || ps[_D1]
  12.                         || ps[_D2] || ps[_D3] ? NAN : INF);
  13.         else if (0 < xchar)
  14.                 ;       /* finite */
  15.         else if ((xchar = _Dnorm(ps)) == 0)
  16.                 return (0);     /* zero */
  17.         lexp = (long)xexp + xchar;
  18.         if (_DMAX <= lexp)
  19.                 {       /* overflow, return +/-INF */
  20.                 *px = ps[_D0] & _DSIGN ? -_Inf._D : _Inf._D;
  21.                 return (INF);
  22.                 }
  23.         else if (0 < lexp)
  24.                 {       /* finite result, repack */
  25.                 ps[_D0] = ps[_D0] & ~_DMASK | (short)lexp << _DOFF;
  26.                 return (FINITE);
  27.                 }
  28.         else
  29.                 {       /* denormalized, scale */
  30.                 unsigned short sign = ps[_D0] & _DSIGN;
  31.  
  32.                 ps[_D0] = 1 << _DOFF | ps[_D0] & _DFRAC;
  33.                 if (lexp < -(48+_DOFF+1))
  34.                         xexp = -1;      /* certain underflow */
  35.                 else
  36.                         {       /* might not underflow */
  37.                         for (xexp = lexp; xexp <= -16; xexp += 16)
  38.                                 {       /* scale by words */
  39.                                 ps[_D3] = ps[_D2], ps[_D2] = ps[_D1];
  40.                                 ps[_D1] = ps[_D0], ps[_D0] = 0;
  41.                                 }
  42.                         if ((xexp = -xexp) != 0)
  43.                                 {       /* scale by bits */
  44.                                 ps[_D3] = ps[_D3] >> xexp
  45.                                         | ps[_D2] << 16 - xexp;
  46.                                 ps[_D2] = ps[_D2] >> xexp
  47.                                         | ps[_D1] << 16 - xexp;
  48.                                 ps[_D1] = ps[_D1] >> xexp
  49.                                         | ps[_D0] << 16 - xexp;
  50.                                 ps[_D0] >>= xexp;
  51.                                 }
  52.                         }
  53.                 if (0 <= xexp && (ps[_D0] || ps[_D1]
  54.                         || ps[_D2] || ps[_D3]))
  55.                         {       /* denormalized */
  56.                         ps[_D0] |= sign;
  57.                         return (FINITE);
  58.                         }
  59.                 else
  60.                         {       /* underflow, return +/-0 */
  61.                         ps[_D0] = sign, ps[_D1] = 0;
  62.                         ps[_D2] = 0, ps[_D3] = 0;
  63.                         return (0);
  64.                         }
  65.                 }
  66.         }
  67.  
  68.